package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanXin.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.AnnouncementVo;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class MessageManager {
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private QuestionService questionService;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private CommentService commentService;
    @DubboReference
    private FriendService friendService;
    @DubboReference
    private AnnouncementService announcementService;
    //打招呼
    public void greetingWay(Long jiaRenUserId, String reply) {
        Long userId = UserHolder.get().getId();
        //环信模板需传入json格式数据 所以创建map集合赋值 最后转为json
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId.toString());
        map.put("nickname", userInfoService.findById(userId).getNickname());
        Question question = questionService.findByUserId(jiaRenUserId);
        if (question == null) {
            question = new Question();
        }
        map.put("strangerQuestion", question.getStrangerQuestion());
        map.put("reply", reply);
        //转换成json格式
        String json = JSON.toJSONString(map);
        //调用环信发送信息
        huanXinTemplate.sendMsg(jiaRenUserId.toString(), json);
    }
    //添加好友
    public void AddContact(long friendId) {
        Long userId = UserHolder.get().getId();
        //调用rpc在mongo中添加好友关系
        friendService.addFriend(userId, friendId);
        //在环信中添加好友关系
        huanXinTemplate.addContacts(userId, friendId);
    }
    //查询好友列表
    public ResponseEntity findContacts(Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();
        //调用rpc查询满足条件的分页类
        PageBeanVo pageBeanVo = friendService.findFriendByPage(userId, pageNum, pageSize);
        //创建vo集合
        List<ContactVo> contactVos = new ArrayList<>();
        //取出分页类中的好友集合 判断非空并遍历
        List<Friend> items = (List<Friend>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Friend item : items) {
                //创建vo类
                ContactVo contactVo = new ContactVo();
                //给vo类赋值
                contactVo.setUserInfo(userInfoService.findById(item.getFriendId()));
                contactVo.setUserId(item.getFriendId().toString());
                //把vo类添加到vo集合
                contactVos.add(contactVo);
            }
        }
        //把vo集合添加到分页类集合中
        pageBeanVo.setItems(contactVos);
        //返回页面
        return ResponseEntity.ok(pageBeanVo);
    }
    //查询点赞、评论、喜欢列表
    public ResponseEntity findCommentList(Integer pageNum, Integer pageSize, Integer commentType) {
        Long userId = UserHolder.get().getId();
        //根据rpc查询满足条件分页类
        PageBeanVo pageBeanVo = commentService.findUserCommentByPage(userId, pageNum, pageSize, commentType);
        //创建vo集合
        List<UserCommentVo> userCommentVos = new ArrayList<>();
        //取出分页类中的点赞、评论、喜欢集合 判断非空并遍历
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Comment item : items) {
                //创建vo类 并赋值
                UserCommentVo userCommentVo = new UserCommentVo();
                UserInfo userInfo = userInfoService.findById(item.getUserId());
                userCommentVo.setAvatar(userInfo.getAvatar());
                userCommentVo.setNickname(userInfo.getNickname());
                userCommentVo.setId(userInfo.getId().toString());
                userCommentVo.setCreateDate(DateUtil.formatDateTime(new Date(item.getCreated())));
                //把vo类添加到vo集合
                userCommentVos.add(userCommentVo);
            }
        }
        //把vo集合赋值给分页类
        pageBeanVo.setItems(userCommentVos);
        //返回页面
        return ResponseEntity.ok(pageBeanVo);
    }
   //查询公告
    public ResponseEntity findAnnouncements(Integer pageNum, Integer pageSize) {
        //调用rpc查询出满足条件的分页类
        PageBeanVo pageBeanVo = announcementService.findAnnouncement(pageNum, pageSize);
        //创建vo集合
        List<AnnouncementVo> announcementVos = new ArrayList<>();
        //取出分页类中的公告集合 判断非空 并遍历
        List<Announcement> announcements = (List<Announcement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(announcements)) {
            for (Announcement announcement : announcements) {
                //创建vo类 赋值
                AnnouncementVo announcementVo = new AnnouncementVo();
                announcementVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated()));
                announcementVo.setDescription(announcement.getDescription());
                announcementVo.setTitle(announcement.getTitle());
                announcementVo.setId(announcement.getId().toString());
                //把vo类添加到vo集合
                announcementVos.add(announcementVo);
            }
        }
        //把vo集合赋值给分页类
        pageBeanVo.setItems(announcementVos);
        //返回页面
        return ResponseEntity.ok(pageBeanVo);
    }
}
